package com.ncloud.crm.dubboservice.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;

import com.ncloud.crm.dubboservice.UserDashboardService;
import com.ncloud.crm.mapper.UserDashboardMapper;

@Service
public class UserDashboardServiceImpl implements UserDashboardService {
	private static final Logger logger = LogManager.getLogger(UserDashboardServiceImpl.class);
	@Autowired 
	UserDashboardMapper mapper;
	
	//账户概览
	@Override
	public JSONObject getCountUserNode(JSONObject json){
		if(!json.containsKey("userID")){
			throw new UserException("2001","传入参数有误");
		}
		String userId = json.getString("userID");
		JSONObject jsonObject=new JSONObject();
		try{
			////公有云节点：统计数字，用户的所有公有云节点数
			int countOpen=0;
			countOpen=mapper.getCountOpenNode(userId);
			jsonObject.put("countOpen", countOpen);
			
			//SWAN节点：统计数字，用户所有的CPE节点数			
			int countSWAN=0;
			countSWAN=mapper.getCountSWANNode(userId);
			jsonObject.put("countSWAN", countSWAN);
			
			//工单：统计数字，用户未处理的工单数
			int countWork=0;
			countWork=mapper.getCountWork(userId);
			jsonObject.put("countWork", countWork);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return jsonObject;		
	}
	//用户所有节点
	@Override
	public JSONArray getOrderInfo(JSONObject json){
		if(!json.containsKey("userID")){
			throw new UserException("2001","传入参数有误");
		}
		String userId = json.getString("userID");
		JSONArray jsonArray =new JSONArray();
		List<Map<Object,Object>> listInfo=new ArrayList<Map<Object,Object>>();
		try{
			listInfo=mapper.getNodeInfo(userId);	
			for(int i=0;i<listInfo.size();i++){
				Map<Object,Object> map=new HashMap<Object, Object>();
				Map<Object,Object> mapInfo=new HashMap<Object, Object>();
				map=listInfo.get(i);
				//名称、类型、带宽
				boolean flag=map.containsKey("attr_value");
				String ispWidth = null;
				if(flag){
					ispWidth=map.get("attr_value").toString();
					}
				
				boolean flag1=map.containsKey("node_name");
				String nodeName = null;
				if(flag1){
					nodeName=map.get("node_name").toString();
					}
							
				boolean flag2=map.containsKey("node_type");
				String nodeType = null;
				if(flag2){
					nodeType=map.get("node_type").toString();
					}
				mapInfo.put("ispWidth",ispWidth);
				mapInfo.put("nodeName",nodeName);
				mapInfo.put("nodeType",nodeType);
				jsonArray.add(mapInfo);			
			}		
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return jsonArray;		
	}
	//节点拓扑
	@Override
	public JSONObject getUserNodeCityByUserId (JSONObject json){		
			if(!json.containsKey("userID")){
				throw new UserException("2001","传入参数有误");
			}
			String userId = json.getString("userID");
			
			JSONObject jsonObject=new JSONObject();
			List<Map<Object,Object>> listInfo=new ArrayList<Map<Object,Object>>();
			try{
				listInfo=mapper.getUserNodeCityByUserId(userId);	
				List<Object> cityNameList1=new ArrayList<Object>();
				List<Object> cityNameList2=new ArrayList<Object>();
				JSONArray jsonArray =new JSONArray();
				for(int i=0;i<listInfo.size();i++){
					Map<Object,Object> map=new HashMap<Object, Object>();
					Map<Object,Object> mapInfo=new HashMap<Object, Object>();
					map=listInfo.get(i);
					boolean flag=map.containsKey("county_name");
					String countyName = null;
					if(flag){
						countyName=map.get("county_name").toString();
						}
					
					boolean flag1=map.containsKey("count");
					String count = null;
					if(flag1){
						count=map.get("count").toString();
						}
					cityNameList1.add(countyName);	
					cityNameList2.add(countyName);	
					mapInfo.put("name",countyName);
					mapInfo.put("value",count);
					jsonArray.add(mapInfo);
				}
				jsonObject.put("point", jsonArray);
				
				//城市连线
				JSONArray jsonArray2 =new JSONArray();
				for(int i=0;i<cityNameList1.size();i++){
					for(int k=i+1;k<cityNameList2.size();k++){
						Map<Object,Object> cityMap = new HashMap<Object, Object>();
						cityMap.put("from",cityNameList1.get(i));
						cityMap.put("to",cityNameList2.get(k));
						jsonArray2.add(cityMap);
					}
				}
				jsonObject.put("line",jsonArray2);
			} catch (Exception e) {
				e.printStackTrace();
				throw new UserException(ResultEnum.DATABASE_ERROR);
			}
			return jsonObject;		
	}
	
}
